package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.feign.WorkOrderV2DynamicFeignClient;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.WorkOrderV2DynamicDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicAddLinkData;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteLinkData;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 新工单关联新建新增工单动态
 * ruansicheng
 * 2022/12/20
 */
@Service("WorkOrderV2DynamicStrategyImpl")
public class WorkOrderV2DynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {

    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private WorkOrderV2DynamicFeignClient workOrderV2DynamicFeignClient;

    private final Logger LOG = LoggerFactory.getLogger(WorkOrderV2DynamicStrategyImpl.class);

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.WORK_ORDER_V2.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
    }

    /**
     * 新增关联数据
     * date: 2022/5/23 2:34 下午
     *
     * @param list:
     */
    @Async("dynamicThreadPool")
    @Override
    public void addLinkData(List<DynamicAddLinkData> list) {
        try {
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            DynamicAddLinkData dynamicAddLinkData = list.get(0);
            String paasFormName = dynamicAddLinkData.getPaasFormName();
            String userId = dynamicAddLinkData.getUserId();
            String corpid = dynamicAddLinkData.getCorpid();
            String name = dynamicAddLinkData.getName();
            String userName = dynamicAddLinkData.getUserName();
            Long linkDataFormId = dynamicAddLinkData.getFormId();
            Long dataId = dynamicAddLinkData.getDataId();
            PaasFormEntity paasFormEntity = paasFormModel.getByKey(linkDataFormId, corpid);
            if (StringUtil.isEmpty(paasFormName)) {
                paasFormName = paasFormEntity.getName();
            }
            List<WorkOrderV2DynamicDTO> dynamicDTOS=new ArrayList<>();
            String finalPaasFormName = paasFormName;
            list.forEach(temp -> {
                Long parentDataId = temp.getParentDataId();
                String memo;
                String info = name;
                memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.WORK_ORDER_V2_DYNAMIC_LINK_DATA_ADD),finalPaasFormName, name);
                WorkOrderV2DynamicDTO workOrderV2DynamicDTO=new WorkOrderV2DynamicDTO();
                workOrderV2DynamicDTO.setCorpid(corpid);
                workOrderV2DynamicDTO.setCreatorId(userId);
                workOrderV2DynamicDTO.setWorkOrderId(parentDataId);
                workOrderV2DynamicDTO.setRefAppId(paasFormEntity.getAppId());
                workOrderV2DynamicDTO.setRefBusinessType(temp.getBusinessType());
                workOrderV2DynamicDTO.setRefId(temp.getDataId());
                workOrderV2DynamicDTO.setRefName(info);
                workOrderV2DynamicDTO.setMemo(memo);
                workOrderV2DynamicDTO.setRefMenuId(paasFormEntity.getMenuId());
                dynamicDTOS.add(workOrderV2DynamicDTO);
            });
            //通过feign调用
            workOrderV2DynamicFeignClient.batchAddDynamic(dynamicDTOS);
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy.addLinkData", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
    }


    @Async("dynamicThreadPool")
    @Override
    public void deleteBatchLinkData(List<DynamicDeleteLinkData> list) {
        try {
            if (CollectionUtils.isEmpty(list)) {
                return;
            }
            DynamicDeleteLinkData dynamicDeleteLinkData = list.get(0);
            Long linkDataFormId = dynamicDeleteLinkData.getFormId();
            String userName = dynamicDeleteLinkData.getUserName();
            // Long dataId = dynamicDeleteLinkData.getDataId();

            String userId = dynamicDeleteLinkData.getUserId();
            String corpid = dynamicDeleteLinkData.getCorpid();
            String paasFormName = dynamicDeleteLinkData.getPaasFormName();
            Integer deleteBusinessType = null;
            PaasFormEntity paasFormEntity = paasFormModel.getByKey(linkDataFormId, corpid);
            if (Objects.nonNull(paasFormEntity)) {
                deleteBusinessType = paasFormEntity.getBusinessType();
                paasFormName = paasFormEntity.getName();
            }
            Integer finalDeleteBusinessType = deleteBusinessType;
            String finalPaasFormName = paasFormName;
            List<WorkOrderV2DynamicDTO> dynamicDTOS=new ArrayList<>();
            list.forEach(temp -> {
                String name = temp.getName();
                Long parentDataId = temp.getParentDataId();
                String memo;
                memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.DYNAMIC_LINK_DATA_DELETE),
                        userName, finalPaasFormName, name);
                WorkOrderV2DynamicDTO workOrderV2DynamicDTO=new WorkOrderV2DynamicDTO();
                workOrderV2DynamicDTO.setCorpid(corpid);
                workOrderV2DynamicDTO.setCreatorId(userId);
                workOrderV2DynamicDTO.setWorkOrderId(parentDataId);
                workOrderV2DynamicDTO.setRefAppId(paasFormEntity.getAppId());
                workOrderV2DynamicDTO.setRefBusinessType(finalDeleteBusinessType);
                workOrderV2DynamicDTO.setRefId(temp.getDataId());
                workOrderV2DynamicDTO.setRefName(name);
                workOrderV2DynamicDTO.setMemo(memo);
                workOrderV2DynamicDTO.setRefMenuId(paasFormEntity.getMenuId());
                dynamicDTOS.add(workOrderV2DynamicDTO);
            });
            workOrderV2DynamicFeignClient.batchDeleteDynamic(dynamicDTOS);
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy.deleteBatchLinkData", e);
        }
    }
}
